package com.ruoyi.info.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.text.Convert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.info.domain.CmsInfo;
import com.ruoyi.info.service.ICmsInfoService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 各种信息配置Controller
 * 
 * @author xwj
 * @date 2023-07-06
 */
@Controller
@RequestMapping("/info/info")
public class CmsInfoController extends BaseController
{
    private String prefix = "info/info";

    @Autowired
    private ICmsInfoService cmsInfoService;

    @GetMapping()
    public String info()
    {
        return prefix + "/info";
    }

    // 友情链接
    @GetMapping("/outLink")
    public String outLink() {
        return prefix + "/outLink";
    }

    @GetMapping("/outLinkAdd")
    public String outLinkAdd() {
        return prefix + "/outLinkAdd";
    }

    @GetMapping("/outLinkEdit/{id}")
    public String outLinkEdit(@PathVariable("id") Long id, ModelMap mmap) {
        CmsInfo cmsInfo = cmsInfoService.selectCmsInfoById(id);
        mmap.put("cmsInfo", cmsInfo);
        return prefix + "/outLinkEdit";
    }

    /** ------------ 轮播图配置 --------- */
    @GetMapping("/swiper")
    public String swpier(){
        return prefix + "/swiper";
    }

    @GetMapping("/swiperAdd")
    public String swiperAdd() {
        return prefix + "/swiperAdd";
    }

    @GetMapping("/swiperEdit/{id}")
    public String swiperEdit(@PathVariable("id") Long id, ModelMap mmap) {
        CmsInfo cmsInfo = cmsInfoService.selectCmsInfoById(id);
        mmap.put("cmsInfo", cmsInfo);
        return prefix + "/swiperEdit";
    }


    /**
     * SEO
     * @param map
     * @return
     */
    @GetMapping("/seo")
    public String seo(Map<String, Object> map) {

        CmsInfo titleEntity = cmsInfoService.getByKey("title");
        CmsInfo subTitleEntity = cmsInfoService.getByKey("subTitle");
        CmsInfo keywordsEntity = cmsInfoService.getByKey("keywords");
        CmsInfo descriptionEntity = cmsInfoService.getByKey("description");
        CmsInfo filingsEntity = cmsInfoService.getByKey("filings");
        CmsInfo bottomInfoEntity = cmsInfoService.getByKey("bottomInfo");
        CmsInfo logoEntity = cmsInfoService.getByKey("logo");

        String title = "";
        String subTitle = "";
        String keywords = "";
        String description = "";
        String filings = "";
        String bottomInfo = "";
        String logo = "";

        if(titleEntity != null) {
            title = titleEntity.getValue();
        }
        if(subTitleEntity != null) {
            subTitle = subTitleEntity.getValue();
        }
        if(keywordsEntity != null) {
            keywords = keywordsEntity.getValue();
        }
        if(descriptionEntity != null) {
            description = descriptionEntity.getValue();
        }
        if(filingsEntity != null) {
            filings = filingsEntity.getExtra();
        }
        if(bottomInfoEntity != null) {
            bottomInfo = bottomInfoEntity.getExtra();
        }
        if(logoEntity != null) {
            logo = logoEntity.getImgUrl();
        }

        map.put("title", title);
        map.put("subTitle", subTitle);
        map.put("keywords", keywords);
        map.put("description", description);
        map.put("filings", filings);
        map.put("bottomInfo", bottomInfo);
        map.put("logo", logo);

        return prefix + "/seo";
    }

    /**
     * 保存配置
     * @return
     */
    @RequestMapping("/saveSeo")
    @ResponseBody
    public AjaxResult saveSeo(@RequestParam Map<String, Object> param) {
        String title = Convert.toStr(param.get("title"));
        String subTitle = Convert.toStr(param.get("subTitle"));
        String keywords = Convert.toStr(param.get("keywords"));
        String description = Convert.toStr(param.get("description"));
        String filings = Convert.toStr(param.get("filings"));
        String bottomInfo = Convert.toStr(param.get("bottomInfo"));
        String logo = Convert.toStr(param.get("logo"));

        cmsInfoService.updateValueBykey("title", title);
        cmsInfoService.updateValueBykey("subTitle", subTitle);
        cmsInfoService.updateValueBykey("keywords", keywords);
        cmsInfoService.updateValueBykey("description", description);

        cmsInfoService.updateExtraBykey("filings", filings);
        cmsInfoService.updateExtraBykey("bottomInfo", bottomInfo);

        CmsInfo logoEntity = new CmsInfo();
        logoEntity.setKeyCode("logo");
        logoEntity.setImgUrl(logo);

        cmsInfoService.updateBykey("logo", logoEntity);


        return AjaxResult.success();
    }

    @RequestMapping("/qrcode")
    public String qrcode(Map<String, Object> map) {
        CmsInfo wxEntity = cmsInfoService.getByKey("wxcode");
        CmsInfo dyEntity = cmsInfoService.getByKey("dycode");
        CmsInfo wbEntity = cmsInfoService.getByKey("wbcode");

        String wxcode = "";
        String dycode = "";
        String wbcode = "";

        if(wxEntity != null) {
            wxcode = wxEntity.getImgUrl();
        }
        if(dyEntity != null) {
            dycode = dyEntity.getImgUrl();
        }
        if(wbEntity != null) {
            wbcode = wbEntity.getImgUrl();
        }

        map.put("wxcode", wxcode);
        map.put("dycode", dycode);
        map.put("wbcode", wbcode);


        return prefix + "/qrcode";
    }


    @RequestMapping("/saveQrcode")
    @ResponseBody
    public AjaxResult saveQrcode(@RequestParam Map<String, Object> param) {
        String wxcode = Convert.toStr(param.get("wxcode"));
        String dycode = Convert.toStr(param.get("dycode"));
        String wbcode = Convert.toStr(param.get("wbcode"));

        CmsInfo wx = new CmsInfo();
        CmsInfo dy = new CmsInfo();
        CmsInfo wb = new CmsInfo();

        wx.setKeyCode("wxcode");
        wx.setImgUrl(wxcode);
        wx.setName("官方微信号");

        dy.setKeyCode("dycode");
        dy.setImgUrl(dycode);
        dy.setName("官方抖音号");

        wb.setKeyCode("wbcode");
        wb.setImgUrl(wbcode);
        wb.setName("官方微博");

        cmsInfoService.updateBykey("wxcode", wx);
        cmsInfoService.updateBykey("dycode", dy);
        cmsInfoService.updateBykey("wbcode", wb);

        return AjaxResult.success();
    }

    /** 首页横幅 */
    @GetMapping("/index_banner")
    public String index_banner(Map<String, Object> map) {
        CmsInfo entity = cmsInfoService.getByKey("indexBanner");
        String imgUrl = "";
        if(entity != null) {
            imgUrl = entity.getImgUrl();
        }
        map.put("imgUrl", imgUrl);
        return prefix + "/index_banner";
    }

    @RequestMapping("/saveIndexBanner")
    @ResponseBody
    public AjaxResult saveIndexBanner(@RequestParam Map<String, Object> param) {
        CmsInfo entity = cmsInfoService.getByKey("indexBanner");
        String imgUrl = Convert.toStr(param.get("imgUrl"));

        if(entity == null) {
            entity = new CmsInfo();
            entity.setKeyCode("indexBanner");
            entity.setDescription("首页横幅");
            entity.setImgUrl(imgUrl);
            cmsInfoService.insertCmsInfo(entity);
        } else {
            entity.setImgUrl(imgUrl);
            entity.setUpdateTime(new Date());
            cmsInfoService.updateCmsInfo(entity);
        }

        return AjaxResult.success();
    }

    /** ------------ 首页-其他横幅 --------- */
    @GetMapping("/banner")
    public String banner(){
        return prefix + "/banner";
    }

    @GetMapping("/bannerAdd")
    public String bannerAdd() {
        return prefix + "/bannerAdd";
    }

    @GetMapping("/bannerEdit/{id}")
    public String bannerEdit(@PathVariable("id") Long id, ModelMap mmap) {
        CmsInfo cmsInfo = cmsInfoService.selectCmsInfoById(id);
        mmap.put("cmsInfo", cmsInfo);
        return prefix + "/bannerEdit";
    }

    /**
     * 查询各种信息配置列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CmsInfo cmsInfo)
    {
        startPage();
        List<CmsInfo> list = cmsInfoService.selectCmsInfoList(cmsInfo);
        return getDataTable(list);
    }

    /**
     * 导出各种信息配置列表
     */
    @Log(title = "各种信息配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CmsInfo cmsInfo)
    {
        List<CmsInfo> list = cmsInfoService.selectCmsInfoList(cmsInfo);
        ExcelUtil<CmsInfo> util = new ExcelUtil<CmsInfo>(CmsInfo.class);
        return util.exportExcel(list, "各种信息配置数据");
    }

    /**
     * 新增各种信息配置
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存各种信息配置
     */
    @Log(title = "各种信息配置", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CmsInfo cmsInfo)
    {
        return toAjax(cmsInfoService.insertCmsInfo(cmsInfo));
    }

    /**
     * 修改各种信息配置
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        CmsInfo cmsInfo = cmsInfoService.selectCmsInfoById(id);
        mmap.put("cmsInfo", cmsInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存各种信息配置
     */
    @Log(title = "各种信息配置", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CmsInfo cmsInfo)
    {
        return toAjax(cmsInfoService.updateCmsInfo(cmsInfo));
    }

    /**
     * 删除各种信息配置
     */
    @Log(title = "各种信息配置", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(cmsInfoService.deleteCmsInfoByIds(ids));
    }
}
